package team8.helpers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;

public class timehelper {

	public String now(String dateFormat) {
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
	    return sdf.format(cal.getTime());

	  }

	public int converttimeseconds (String time)
	{
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		
		int counter =tokenizer.countTokens();

		
		int res=0;
		while (tokenizer.hasMoreTokens())
		{
			if (counter==3)
			{
				res+= Integer.valueOf(tokenizer.nextToken())*60*60;
			}
			else
				if (counter==2)
				{
					res+= Integer.valueOf(tokenizer.nextToken())*60;
				}
				else
				{
					res+= Integer.valueOf(tokenizer.nextToken());
				}
			counter--;
		}
		return res;
	}
	
	public double converttimeminutes (String time)
	{
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		int counter =tokenizer.countTokens();
		
		double res=0;
		while (tokenizer.hasMoreTokens())
		{
			if (counter==3)
			{
				res+= Integer.valueOf(tokenizer.nextToken())*60;
			}
			else
				if (counter==2)
				{
					res+= Integer.valueOf(tokenizer.nextToken());
				}
				else
				{
					res+= (Double.valueOf(tokenizer.nextToken())/60);
				}
			counter++;
		}
		return res;
	}
	
	public String secondstotime (int s)
	{
		   int hours = s/3600;
		  int minutes = ((s-(hours*3600))/60);
		  int seconds = (s-(hours*3600)-(minutes*60));
		
		  String timeres="";
		  if (hours<10)
			  	timeres+="0"+hours+":";
		  else
			  	timeres+=hours+":";
	      if (minutes<10)
	    	  timeres+="0"+minutes+":";
		  else
			  	timeres+=minutes+":";
	      if (seconds<10)
	    	  timeres+="0"+seconds;
		  else
			  	timeres+=seconds;
	      
		  return timeres;
	}
}
